
<template>
  <div class="users">
    <div class="top">
      <el-input
        placeholder="请输入内容"
        v-model="page.query"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchVal"
        ></el-button>
      </el-input>
      <el-button type="primary" @click="userBtn">添加用户</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column prop="username" label="姓名"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="role_name" label="角色"> </el-table-column>
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.mg_state" @change="statusFn(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button type="warning" icon="el-icon-setting" circle @click="editRoles(scope.row)"></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="delUser(scope.row)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      class="el-pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.pagenum"
      :page-sizes="[2, 5, 10, 15]"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加用户 -->
    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="userAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑用户 -->
    <el-dialog title="编辑用户" :visible.sync="dialogVisible1" width="30%">
      <el-form
        :model="ruleForm1"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm1.username" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm1.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="ruleForm1.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="changeEdit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配弹窗 -->
    <el-dialog title="分配角色" :visible.sync="dialogVisible2" width="30%">
      <el-form
        :model="ruleForm1"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm1.username" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm1.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="ruleForm1.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="changeEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// 请求数据
import {
  getUsers,
  postUsers,
  delUsers,
  editUsers,
  editStatus,
} from "../../http/user";
import _ from "lodash";
export default {
  name: "",
  components: {},
  data() {
    return {
      page: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      total: 0,
      tableData: [],
      list: [],
      dialogVisible: false, //弹出框
      dialogVisible1: false, //编辑用户弹出框
      dialogVisible2:false, //分配角色弹出框
      // 添加用户
      ruleForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      editId: 0,
      ruleForm1: { username: "", email: "", mobile: "" },
      //  校验
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入电话", trigger: "blur" }],
      },
    };
  },
  watch:{
   tableData:{
    handler(n){
    if(this.page.pagenum>1&&n.length==0){
     this.page.pagenum-=1
      this.render();
    }
    },
    deep:true,
    immediate:true
   } 
  },
  created() {
    this.render();
  },
  mounted() {},
  methods: {
    render() {
      getUsers({ params: this.page }).then((res) => {
        console.log(res);
        if (res.meta.status == 200) {
          this.$message({
            message: "获取管理员列表成功",
            type: "success",
          });
          this.tableData = res.data.users;
          this.total = res.data.total;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page.pagesize = val;
      this.render();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pagenum = val;
      this.render();
    },
    // 添加用户按钮
    userBtn() {
      this.dialogVisible = true;
    },
    // 确定按钮
    userAdd() {
      postUsers(this.ruleForm).then((res) => {
        console.log(res);
        this.render();
      });
      this.dialogVisible = false;
    },
    // 删除
    delUser(val) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUsers({ id: val.id }).then((res) => {
            console.log(res);
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.render();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 搜索
    // searchVal() {
    //   this.page.pagenum = 1;
    //   this.render();
    // },
    // 搜索节流
    searchVal: _.debounce(function () {
      this.page.pagenum = 1;
      this.render();
    },2000),
    // 编辑用户
    handleEdit(val) {
      this.dialogVisible1 = true;
      this.ruleForm1 = val;
      this.editId = val.id;
    },
    // 编辑用户确定按钮
    changeEdit() {
      editUsers({
        id: this.editId,
        email: this.ruleForm1.email,
        mobile: this.ruleForm1.mobile,
      }).then((res) => {
        if (res.meta.status == 200) {
          console.log(res);
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.dialogVisible1 = false;
        }
      });
    },
    // 修改用户状态
    async statusFn(row) {
      let { id, mg_state } = row;
      let res = await editStatus(id, mg_state);
      console.log(res);
      if (res.meta.status == 200) {
        this.$message({
          message: "状态修改成功",
          type: "success",
        });
      }
    },
    // 分配用户角色
    editRoles(){
      this.dialogVisible2=true
    }
  },
};
</script>
<style scoped lang='scss'>
.users {
  padding: 10px 15px;
  margin-top: 10px;
  background-color: #fff;
  .top {
    margin: 10px 0;
    display: flex;
    align-items: center;
    .input-with-select {
      width: 400px;
      margin-right: 40px;
    }
  }
  .el-pagination {
    text-align: left;
    margin: 20px 0;
  }
}
</style>
